--
-- NewTeamImpeachCaptain.lua
-- Created by IntelliJ IDEA.
-- User: ema
-- Date: 2018/11/29
--

local NewTeamMemberHeadRender = import(".renders.NewTeamMemberHeadRender")
local TeamModel = app:getInst("TeamModel") -- 此处虽然包含了 TeamModel 但是只调用了其静态成员函数

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

local string_format = string.format

ClassRef.VIEW_ROOT_RES = Res.NewTeamBeInvitedView

function ClassRef:init(data)
    display.nodeRemoveForNotHit( self._ui["Panel"], function()
        self._isCloseFlag = true
        executeFunc(self._onOperate, "ImpeachResponses", {ChangeUUID = self._inviteUUID, agree = false})
        executeFunc(self._onCloseView, self)
    end )

    self._confirmButton = self._ui["Panel/Button_queding"]
    self._confirmButton:addClickEventListener(function()
        self._isCloseFlag = true
        executeFunc(self._onOperate, "ImpeachResponses", {ChangeUUID = self._inviteUUID, agree = true})
        executeFunc(self._onCloseView)
    end)
    self._ui["Panel/Button_quxiao"]:addClickEventListener(function()
        self._isCloseFlag = true
        executeFunc(self._onOperate, "ImpeachResponses", {ChangeUUID = self._inviteUUID, agree = false})
        executeFunc(self._onCloseView)
    end)

    self._InviterHeadIcon = NewTeamMemberHeadRender.create(self._ui["Panel/head"])
    self._InviterHeadIcon:onClick(function(sender)
--        local size = sender:getContentSize()
--        local pos = sender:convertToWorldSpace(cc.p(size.width, size.height/2))
--        UIHandler:showHeadMenu({online=true, attr=self._data.inviterInfo, pos=pos})
    end)

    self._isCloseFlag = false
end

-- params = S2C_ChangeTeamLeaderInfo pb 结构
-- team = TeamVo
function ClassRef:refreshContents(params, team)
    self._data = params
    self._inviteUUID = params.ChangeUUID
    self._InviterHeadIcon:setData(params.inviterInfo)

    -- 队伍目标展示
    local teamTarget = team:getTeamTargetVo()
    self._ui.target:setVisible(teamTarget ~= nil)
    if teamTarget then
        self._ui.target:setString(string.format(L("lua_code_text_149"), teamTarget:getFullName()))
    end

    self._tickTime = tonumber(GD:getSystemPar("changeLeaderTime", 30))
    local function _tick()
        if cc.isDead(self) then return end

        if self._tickTime <= 1 or self._isCloseFlag then
            executeFunc(self._onCloseView)
        else
            self._confirmButton:getChildByName("name"):setString(string_format(L("lua_code_text_524"), self._tickTime))
            self._tickTime = self._tickTime - 1
        end
    end

    local inviterTeamGrade = L("lua_code_text_189")
    if team and team:isLegion() then
        inviterTeamGrade = L("lua_code_text_327")
    end

    self._ui["Panel/tlt"]:setString(L("text_10500047"))
    -- 邀请人信息
    self._ui["Panel/tips"]:setString(string.format(L("text_10500045"), params.inviterInfo.name, inviterTeamGrade))
    -- 成员个数
    self._ui["Panel/num"]:setVisible(false)
    -- 战斗力
    self._ui["Panel/combat"]:setString(string.format(L("text_10500046"), params.inviterInfo.combatPower))

    self._countDown = scheduler.newCron(_tick, 1, true)
    _tick()
end

function ClassRef:removeScheduler()
    if self._countDown then
        scheduler.cancelCron(self._countDown)
        self._countDown = nil
    end
end


return ClassRef
